---
title: "Experiment 2b"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r load libraries}
library(readr)
library(reshape2)
library(plyr)
library(ggplot2)
library(mediation)
library(tidyverse)
library(stringr)
```

```{r import}
fulldata <- read_csv("~/Desktop/Studies/Force/analyses/FINAL/Exp.2/exp2b_completefinaldata_foranalysis.csv", 
    col_names = FALSE)


```
##Clean data
```{r clean_data}
#Name the columns
names(fulldata)<-c("sub","duration","captcha","LLH_LHH","alt1_norm","alt2_norm","alt1_val","alt2_val","force","comp1","comp2","sub2","condition2","actual_act","alt1","alt2","comp3_resp","comp3")

#Remove rows with any missing data
cleandata<-fulldata[complete.cases(fulldata), ]

#Specify that the variables are factors
cleandata$sub<-factor(cleandata$sub)
cleandata$LLH_LHH<-factor(cleandata$LLH_LHH, labels=c("Normal Alternative","Abnormal Alternative"))

#create new alt variable that is the lowest of the two alt norm responses and #highest of the two alt val responses
cleandata$critalt_norm<-ifelse(cleandata$alt1_norm < cleandata$alt2_norm,cleandata$alt1_norm,cleandata$alt2_norm)

cleandata$critalt_val<-ifelse(cleandata$alt1_val > cleandata$alt2_val,cleandata$alt1_val,cleandata$alt2_val)

```
##Get descriptives
```{r descriptives}

#get descriptives
force_desc <- ddply(cleandata, c("LLH_LHH"), summarise,
               N    = length(force),
               mean = mean(force),
               sd   = sd(force),
               se   = sd / sqrt(N)
)
print(force_desc)

norm1_desc <- ddply(cleandata, c("LLH_LHH"), summarise,
               N    = length(critalt_norm),
               mean = mean(critalt_norm),
               sd   = sd(critalt_norm),
               se   = sd / sqrt(N)
)
print(norm1_desc)

value1_desc <- ddply(cleandata, c("LLH_LHH"), summarise,
               N    = length(critalt_val),
               mean = mean(critalt_val),
               sd   = sd(critalt_val),
               se   = sd / sqrt(N)
)
print(value1_desc)

```
###Final Plots

```{r finalplots}
#Import desisl_1 data
desisl_1data <- read_csv("~/Dropbox/Mac/Desktop/Studies/Force/analyses/Final/Exp.2/exp2a_data.csv", 
    col_names = FALSE)

names(desisl_1data)<-c("sub","LLH_LHH","had_did","keep_rej","neg_pos","resp")
#Specify which variables are factors are factors)

#clean desisl_1data
desisl_1data$had_did<-factor(desisl_1data$had_did,labels=c("had","did"))
desisl_1data$keep_rej<-factor(desisl_1data$keep_rej,labels=c("keep","rej"))
desisl_1data$neg_pos<-factor(desisl_1data$neg_pos,labels=c("neg","pos"))
desisl_1data$LLH_LHH<-factor(desisl_1data$LLH_LHH, labels=c("Normal Alternative","Abnormal Alternative"))

keep_data<-desisl_1data[which(desisl_1data$keep_rej=="keep"),]
rej_data<-desisl_1data[which(desisl_1data$keep_rej=="rej"),]
neg_keep_data<-keep_data[which(keep_data$neg_pos=="neg"),]
pos_keep_data<-keep_data[which(keep_data$neg_pos=="pos"),]
neg_rej_data<-rej_data[which(rej_data$neg_pos=="neg"),]
pos_rej_data<-rej_data[which(rej_data$neg_pos=="pos"),]

#reverse score neg responses
neg_keep_data$resp<-6-neg_keep_data$resp
neg_rej_data$resp<-6-neg_rej_data$resp
#group all of the keep and reject data back together
full_keep_data<-rbind(pos_keep_data,neg_keep_data)
full_rej_data<-rbind(pos_rej_data,neg_rej_data)

#Recode the condition variable for keep and reject games (because LLH is an abnormal alternative in the keep game and a normal alternative in the reject game)
full_keep_data$LLH_LHH<-factor(full_keep_data$LLH_LHH, labels=c("Abnormal Alternative","Normal Alternative"))
full_rej_data$LLH_LHH<-factor(full_rej_data$LLH_LHH, labels=c("Normal Alternative","Abnormal Alternative"))

#pull out just the had trials and combine into a single study 1 dataset
#combine keep and reject data
full_data<-rbind(full_keep_data,full_rej_data)
#isolate just the had trials
full_had_data<-full_data[which(full_data$had_did=="had"),]

#prepare the two studies datasets for merging by eliminating the unnecessary columns
study1a_mergable <- full_had_data[c("sub", "LLH_LHH", "resp")]
study1a_mergable <- dplyr::rename(study1a_mergable, "force" = "resp")
#change the order of normal vs. abnormal alternative for purposes of plotting
study1a_mergable$LLH_LHH = factor(study1a_mergable$LLH_LHH, levels = c("Normal Alternative", "Abnormal Alternative"))

#Add in experiment number for graphing
study1a_mergable$study_num <- "Experiment 2a"

#Extract the study 2 data for graphing
study2a_mergable <- cleandata[c("sub", "LLH_LHH", "force")]
#And add in the study number
study2a_mergable$study_num <- "Experiment 2b"

#merge the datasets (ended up not needing to do this because I ended up creating two separate bar charts for studies 1a and 1b)
merged_data <- rbind(study1a_mergable,study2a_mergable)

###FINAL BAR CHARTS####
library(scales)
cond_legend_title<-"Alternative Action"
exp2_fig1a_1<-study1a_mergable %>%
  ggplot(aes(study_num, force, fill=LLH_LHH)) +
  geom_bar(stat="summary", fun.y='mean', position='dodge') +
  geom_errorbar(stat='summary', width=.2,
                 position=position_dodge(.9)) +
  geom_point(aes(study_num, color=LLH_LHH), size=2,
             position=position_jitterdodge(dodge.width=.9,jitter.width = .4), alpha = .5) +
             scale_color_manual(cond_legend_title, values = c("Normal Alternative" = "#8a433e", "Abnormal Alternative" = "#017073")) +
  scale_fill_manual(cond_legend_title, values = c("Normal Alternative" = "#F8766D", "Abnormal Alternative" = "#00BFC4")) +
  xlab("") +
  ylab(str_wrap("How strongly do you agree that the agent was forced?", 75)) +
  scale_y_continuous(limits = c(1,5), oob = rescale_none, labels = str_wrap(c("Strongly Disagree","2","3","4","Strongly Agree"),10)) + 
  theme_bw() +
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=20, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "right",
        legend.text=element_text(size=14),
        legend.title=element_text(size=16))

ggsave("exp2_fig1a_1.jpg",height = 10, width= 7, dpi=600)

exp2_fig1a_2<-study2a_mergable %>%
  ggplot(aes(study_num, force, fill=LLH_LHH)) +
  geom_bar(stat="summary", fun.y='mean', position='dodge') +
  geom_errorbar(stat='summary', width=.2,
                 position=position_dodge(.9)) +
  geom_point(aes(study_num, color=LLH_LHH), size=2,
             position=position_jitterdodge(dodge.width=.9,jitter.width = .4), alpha = .5) +
             scale_color_manual(cond_legend_title, values = c("Normal Alternative" = "#8a433e", "Abnormal Alternative" = "#017073")) +
  scale_fill_manual(cond_legend_title, values = c("Normal Alternative" = "#F8766D", "Abnormal Alternative" = "#00BFC4")) +
  xlab("") +
  ylab(str_wrap("How strongly do you agree that the agent was forced?", 75)) +
  scale_y_continuous(breaks=waiver(), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  theme_bw() +
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=20, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "right",
        legend.text=element_text(size=14),
        legend.title=element_text(size=16))

ggsave("exp2_fig1a_2.jpg",height = 10, width= 7, dpi=600)

##Final Scatter Plots##
exp2_fig1b<-ggplot(cleandata,aes(y=force,x=critalt_val))+
  geom_point()+
  geom_smooth(method=lm) + 
  xlab(str_wrap("How strongly do you agree that it would have been A GOOD IDEA to do something else instead?", 50)) + 
  scale_x_continuous(breaks=waiver(), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  ylab(str_wrap("How strongly do you agree that the agent was forced?", 30)) +
  scale_y_continuous(breaks=waiver(), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title.x = element_text(size=16, vjust=-1),
        axis.title.y = element_text(size=16, vjust=.35),
        panel.background = element_blank(),
        axis.line = element_line(colour = "grey"),
        #panel.grid = element_blank(),
        legend.position = "none")

ggsave("exp2_fig1b.jpg",height = 5, width= 8, dpi=600)

exp2_fig1c<-ggplot(cleandata,aes(y=force,x=critalt_norm))+
  geom_point()+
  geom_smooth(method=lm) + 
  xlab(str_wrap("How strongly do you agree that it would have been UNUSUAL to do something else instead?", 50)) + 
  scale_x_continuous(breaks=c(0, 25, 50, 75, 100), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  ylab(str_wrap("How strongly do you agree that the agent was forced?", 30)) +
  scale_y_continuous(breaks=c(0, 25, 50, 75, 100), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title.x = element_text(size=16, vjust=-1),
        axis.title.y = element_text(size=16, vjust=.35),
        panel.background = element_blank(),
        axis.line = element_line(colour = "grey"),
        #panel.grid = element_blank(),
        legend.position = "none")

ggsave("exp2_fig1c.jpg",height = 5, width= 8, dpi=600)
```
##Final Analyses
```{r testing effect of condition echo=FALSE}
#Subject level analysis: Predict force judgments by condition. 
summary(lm(force ~ LLH_LHH, data=cleandata))
```

```{r testing effect of normality echo=FALSE}
# Run full model with the critical alternative actions controlling for condition 
summary(lm(force ~ critalt_norm + critalt_val + LLH_LHH, data=cleandata ))

# Run full model with the critical alternative actions without condition
summary(lm(force ~ critalt_norm + critalt_val, data=cleandata ))

fig_nv <- cleandata %>% 
              ggplot(aes(x=critalt_val,y=critalt_norm))+
              geom_point()+
              geom_smooth(method=lm)+
              theme_bw()
```

